package com.huafv.flyauth.web.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huafv.flyauth.common.dto.SysMenuDto;
import com.huafv.flyauth.core.domain.TreeNode;
import com.huafv.flyauth.web.admin.entity.SysMenu;
import com.huafv.flyauth.web.admin.mapper.SysMenuMapper;
import com.huafv.flyauth.web.admin.service.SysMenuService;
import com.huafv.flyauth.web.admin.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysUserService userService;

    @Override
    public List<String> getPermsByUserId(Long userid) {

        List<String> oldPerms = sysMenuMapper.selectPermsByUserId(userid);

        List<String> perms = new ArrayList<>();

        for (String oldPerm : oldPerms) {
            if (oldPerm != null) {
                String[] temp = oldPerm.split(",");
                perms.addAll(Arrays.asList(temp));
            }
        }


        return perms;
    }

    @Override
    public List<SysMenuDto> getCurrentUserMenu(String username) {
        // 通过用户名查询用户菜单
        List<SysMenuDto> sysMenuDtos = sysMenuMapper.selectMenuByUsername(username);

        // 构建菜单树形结构
        return buildMenuTree(sysMenuDtos, 0L);
    }

    @Override
    public List<SysMenu> allMenusTree() {
        List<SysMenu> sysMenus = sysMenuMapper.selectAllMenus();
        // 转换为树
        return buildAllMenuTree(sysMenus, 0L);
    }

    @Override
    public SysMenu getMenuInfo(Long menu_id) {
        return sysMenuMapper.getMenuInfo(menu_id);
    }

    @Override
    public int saveMenuInfo(Map<String, Object> objectMap) {
        SysMenu m = new SysMenu();

        m.setCreated(LocalDateTime.now());
        m.setState((Integer) objectMap.get("state"));
        m.setParentId(((Integer) objectMap.get("parentId")).longValue());
        m.setName((String) objectMap.get("name"));
        m.setPath((String) objectMap.get("path"));
        m.setComponent((String) objectMap.get("component"));
        m.setPermCode((String) objectMap.get("permCode"));
        m.setType((Integer) objectMap.get("type"));
        m.setIcon((String) objectMap.get("icon"));
        m.setOrder((Integer) objectMap.get("order"));

        sysMenuMapper.saveMenuInfo(
                m.getCreated(), m.getState(),
                m.getParentId(), m.getType(), m.getOrder(),
                m.getName(), m.getPath(),m.getComponent(),
                m.getPermCode(), m.getIcon()
        );
        return 0;
    }

    @Override
    public int updateMenuInfo(Map<String, Object> objectMap) {
        //用下面这行代码代替原来的代码Long id = (Long) objectMap.get("id");,直接转换为Long类型
        Long id = ((Integer) objectMap.get("id")).longValue();
        SysMenu m = sysMenuMapper.getMenuInfo(id);

        m.setUpdated(LocalDateTime.now());
        m.setState((Integer) objectMap.get("state"));

        m.setParentId(((Integer) objectMap.get("parentId")).longValue());
        m.setName((String) objectMap.get("name"));
        m.setPath((String) objectMap.get("path"));
        m.setComponent((String) objectMap.get("component"));
        m.setPermCode((String) objectMap.get("permCode"));
        m.setType((Integer) objectMap.get("type"));
        m.setIcon((String) objectMap.get("icon"));
        m.setOrder((Integer) objectMap.get("order"));

        int i = sysMenuMapper.updateMenuInfo(
                m.getId(), m.getUpdated(), m.getState(),
                m.getParentId(), m.getType(), m.getOrder(),
                m.getName(), m.getPath(),m.getComponent(),
                m.getPermCode(), m.getIcon()
        );

        return i;
    }

    @Override
    public List<TreeNode> allMenusTreeNode() {
        List<SysMenu> sysMenus = sysMenuMapper.selectAllMenus();
        List<SysMenu> sysMenuTree = buildAllMenuTree(sysMenus, 0L);
        return convertToTreeNodeList(sysMenuTree);
    }

    private List<SysMenu> buildAllMenuTree(List<SysMenu> sysMenus, Long parentId) {
        List<SysMenu> treeList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            if (parentId.equals(sysMenu.getParentId())) {
                sysMenu.setChildren(buildAllMenuTree(sysMenus, sysMenu.getId()));
                treeList.add(sysMenu);
            }
        }
        return treeList;
    }

    public List<TreeNode> convertToTreeNodeList(List<SysMenu> sysMenus) {
        List<TreeNode> treeNodes = new ArrayList<>();
        for (SysMenu sysMenu : sysMenus) {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(sysMenu.getId());
            treeNode.setLabel(sysMenu.getName());
            List<SysMenu> children = sysMenu.getChildren();
            if (children != null && !children.isEmpty()) {
                treeNode.setChildren(convertToTreeNodeList(children));
            }
            treeNodes.add(treeNode);
        }
        return treeNodes;
    }


    /**
     * 构建菜单树形结构
     *
     * @param sysMenuDtos 菜单列表
     * @param parentId    父级菜单ID
     * @return 菜单树形结构
     */
    private List<SysMenuDto> buildMenuTree(List<SysMenuDto> sysMenuDtos, Long parentId) {
        List<SysMenuDto> treeList = new ArrayList<>();
        for (SysMenuDto sysMenuDto : sysMenuDtos) {
            if (parentId.equals(sysMenuDto.getParentId())) {
                SysMenuDto menuDto = new SysMenuDto();

                menuDto.setTitle(sysMenuDto.getTitle());
                menuDto.setName(sysMenuDto.getName());
                menuDto.setComponent(sysMenuDto.getComponent());
                menuDto.setIcon(sysMenuDto.getIcon());
                menuDto.setPath(sysMenuDto.getPath());
                menuDto.setIndex(sysMenuDto.getIndex());

                menuDto.setChildren(buildMenuTree(sysMenuDtos, sysMenuDto.getId()));
                treeList.add(menuDto);
            }
        }
        return treeList;
    }

}
